package com.gpoint.rdp.door.home.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gpoint.rdp.base.core.util.BeanUtils;

import com.gpoint.rdp.base.web.controller.GenericController;
import com.gpoint.rdp.door.CurrentPermission.service.CurrentPermissionQueryService;
import com.gpoint.rdp.door.door.domain.Door;
import com.gpoint.rdp.door.door.persistence.entity.DoorPo;
import com.gpoint.rdp.door.door.service.DoorQueryService;
import com.gpoint.rdp.door.door.service.DoorService;
import com.gpoint.rdp.door.logs.persistence.entity.SwipeRecordPo;
import com.gpoint.rdp.door.logs.service.SwipeRecordQueryService;
import com.gpoint.rdp.door.zoneCTL.persistence.entity.DoorControllerPo;
import com.gpoint.rdp.door.zoneCTL.service.DoorControllerQueryService;


// TODO: Auto-generated Javadoc
/**
 * The Class RecordController.
 * @author WIN10
 */
@Controller
@RequestMapping("/rdp/door/graph/")
public class RecordController extends GenericController {
	
	/** The swipe record query service. */
	@Resource
	private SwipeRecordQueryService swipeRecordQueryService;
	
	/** The door controller query service. */
	@Resource
	private DoorControllerQueryService doorControllerQueryService;
	

	
	@Resource
	private DoorService doorService;
	
	
	@Resource
	private DoorQueryService DoorQueryService;
	
	
	
	@Resource
	private CurrentPermissionQueryService currentPermissionQueryService;
	
	/**
	 * List.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("index")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView("/rdp/home/recordService.jsp");
		List<SwipeRecordPo> pos = swipeRecordQueryService.queryDayCount();
		Map<String,Integer> countMap = getMounthCount(pos);
		int monthCount =  countMap.get("monthCount");
//		int nowCount =  countMap.get("nowCount");
		//门总数计算
		String json = DoorQueryService.findAll();
		List<DoorPo> list = DoorPo.fromJsonArrayString(json);
		int doorCount = BeanUtils.isEmpty(list)?0:list.size();
		//当前授权人数
		int nowCount=currentPermissionQueryService.getCurrentAuthNumber();
        mv.addObject("dayCountList", pos);
        mv.addObject("monthCount", monthCount);
        //当前授权人数
        mv.addObject("nowCount", nowCount);
        //门总数
        mv.addObject("deviceCount", doorCount);
		return mv;

	}

	/**
	 * Gets the mounth count.
	 *
	 * @param pos the pos
	 * @return the mounth count
	 */
	private Map<String,Integer> getMounthCount(List<SwipeRecordPo> pos) {
		int monthCount = 0;
		int nowCount = 0;
		Map<String,Integer> map = new HashMap<String,Integer>(100);
		map.put("monthCount", monthCount);
		map.put("nowCount", nowCount);
		if (BeanUtils.isEmpty(pos)) {
			return map;
		}
        String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		for (SwipeRecordPo po : pos) {
			monthCount += po.getPassCount();
			if(now.equals(po.getDateStr())) {
				nowCount = po.getPassCount();
			}
		}
		
		if(nowCount==0) {
			SwipeRecordPo r = new SwipeRecordPo();
			r.setDateStr(now);
			r.setPassCount(nowCount);
			pos.add(r);
		}
		map.put("monthCount", monthCount);
		map.put("nowCount", nowCount);
		return map;
	}
	
	
}